package com.lsh.netty.http.xml;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @author ：LiuShihao
 * @date ：Created in 2021/6/8 8:41 下午
 * @desc ：客户端业务处理类
 */
public class HttpXmlClientHandle extends SimpleChannelInboundHandler<HttpXmlResponse> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        // 给客户端发送请求消息，HttpXmlRequest包含FullHttpRequest和Order这个了类
        HttpXmlRequest request = new HttpXmlRequest(null, OrderFactory.create(1002));
        System.out.println("客户端发送请求~");
        ctx.writeAndFlush(request);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        System.out.println("客户端处理异常！");
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    protected void messageReceived(ChannelHandlerContext ctx, HttpXmlResponse msg) throws Exception {
        System.out.println("========客户端接收响应========");
        System.out.println("客户端收到的响应头: " + msg.getHttpResponse().headers().names());
        System.out.println("客户端收到的响应内容: " + msg.getResult());
    }
}
